/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edi.runtime.component
{
	import com.edi.runtime.Event.EDIEventProxy;
	
	import mx.containers.TitleWindow;
	import mx.core.Application;
	import mx.events.CloseEvent;
	
	/**
	 *  TitleWindow 布局容器为其子项提供了标题栏、标注、边框和内容区域。通常，使用 TitleWindow 容器封装独立的应用程序模块。例如，您可以在 TitleWindow 容器中包含一个表单。当用户完成此表单后，您可以通过编程方式关闭 TitleWindow 容器，或允许用户使用“关闭”按钮将其关闭
	 * 
	 */	
	public class EDITitleWindow extends TitleWindow
	{
		private var _parentApp:Application;
		private var _ds:EDIDataSource;
		
		private var _appName:String;
		
		private var params:Object;
		
		public var autoSize:Boolean=false;
		public var closeHandler:Function;
		
		public function EDITitleWindow() 
		{
			super();
			
			this.setStyle("dropShadowEnabled",true);
			this.setStyle("borderAlpha",1);
			this.setStyle("cornerRadius",0);
			
			showCloseButton=true; 
			this.layout="absolute";  
			
			_ds=new EDIDataSource(this);
			
			this.params = new Object();
			
			this.addEventListener(CloseEvent.CLOSE,closeWindowHandler);
		}
		
		protected function get ds():EDIDataSource
		{
			return _ds;
		}
		
		/**
		 * 应用名 
		 * @param value
		 * 
		 */		
		public function set appName(value:String):void{
			_appName=value;
			ds.appName=value;
		}
		public function get appName():String{return _appName==null?"":_appName;}
		
		/**
		 * 自动提交 
		 * @param value
		 * 
		 */		
		public function set dsCommit(value:Boolean):void{
			ds.autoCommit=value;
		}
		public function get dsCommit():Boolean{return ds.autoCommit;}
		
		/**
		 * 主应用Application
		 * @param value
		 * 
		 */		
		public function set parentApp(value:Application):void{_parentApp=value;}
		public function get parentApp():Application{return _parentApp;}
		
		/**
		 * 显示该titleWindow 
		 * 
		 */		
		public function showModel():void
		{
			if(parent!=null)
			{
				parentApp.removeChild(this);
			}			
			var Posx:Number;
			var Posy:Number;
			this.showCloseButton=true;
			if(parentApp==null)
			{
				parentApp=Application(Application.application);
			}
			if(autoSize)
			{
				if(width>parentApp.width){width=parentApp.width-10;}
				if(height>parentApp.height){height=parentApp.height-10;}
			}
			Posx=(parentApp.width-this.width)/2;
			Posy=(parentApp.height-this.height)/2;
			this.x = Posx;
			this.y = Posy;
			
			EDIPopUpManager.addPopUp(this,parentApp,true);
		}
		
		public function addParam(key:String,value:Object):void{
			this.params[key] = value;
		}
		
		public function getParam(key:String):Object{
			return this.params[key];
		}
		
		
		protected function closeWindowHandler(event:CloseEvent):void
		{
			com.edi.runtime.Event.EDIEventProxy.removeListener(this);
			
			close();

			//this.removeEventListener(CloseEvent.CLOSE,closeWindowHandler);

			//this.removeAllChildren();
		}
		
		/**
		 * 关闭该titleWindow 
		 * 
		 */		
		public function close():void
		{
			EDIPopUpManager.removePopUp(this);
			if(closeHandler!=null)
			{
				closeHandler.call();
			}
		}
	}
}